package com.wnnight.wys.mapper.car;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wnnight.wys.entity.car.Park;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;

/**
 * @ClassName: ParkMapper
 * @Description: 车位Mapper
 * @author: CHX42
 * @date: 2022/8/27-21:29
 * @version: v1.0
 */
public interface ParkMapper extends BaseMapper<Park> {
    @SelectProvider(type = ParkSql.class, method = "getAll")
    List<Park> getAll(Park park);

    @SelectProvider(type = ParkSql.class, method = "getByLandlordIds")
    List<Park> getByLandlordIds(Integer[] ids);

    @Select("SELECT *  FROM wys_park LEFT JOIN wys_landlord ON wys_park.landlord_id=wys_landlord.id  ${ew.customSqlSegment}"  )
    Page<Park> getPage(IPage<Park> page, @Param("ew") Wrapper wrapper);


    @Slf4j
    class ParkSql {
        public static String getAll(Park park) {
            String sql = new SQL() {{
                SELECT("*");
                FROM("wys_park");
                if (park.getStatus()!=null) {
                   WHERE("status=#{status}");
                }
                if (park.getType()!=null) {
                    WHERE("type=#{type}");
                }
                if (park.getParkNum()!=null) {
                    WHERE("park_num like '%${parkNum}%'");
                }
            }}.toString();
            log.info("getAll->SQL:{}", sql);
            return sql;
        }
        public static String getByLandlordIds(Integer[] ids) {
            String sql = new SQL() {{
                SELECT("*");
                FROM("wys_park");
                for (Integer id:ids) {
                    WHERE("landlord_id="+id);
                    OR();
                }

            }}.toString();
            log.info("getAll->SQL:{}", sql);
            return sql;
        }
    }
    @Select("select * from ${prefix}park where landlord_id=#{landlordId}")
    @Results({
            @Result(column = "id",property = "id",id = true),
            @Result(column = "park_num",property = "parkNum"),
    })
    List<Park> getByLandlordId(int landlordId);
}

